import multiprocessing
import time

# CPU 密集型任务
def cpu_intensive_task():
    total = 0
    for i in range(10**7):
        total += i
    print(f"Total: {total}")

if __name__ == '__main__':
    # 创建多个进程
    processes = []
    start_time = time.time()

    for _ in range(4):  # 创建 4 个进程
        process = multiprocessing.Process(target=cpu_intensive_task)
        processes.append(process)
        process.start()

    # 等待所有进程完成
    for process in processes:
        process.join()

    end_time = time.time()
    print(f"Time taken with processes: {end_time - start_time:.2f} seconds")